﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Al.UI.Controls
{
    public abstract class CometPage: Page, ICometPage
    {
        private CometController _cometController;

        protected override void OnInit(EventArgs e)
        {
            //Initialize the Controller
            this.InitializeController();

            //Add the postback panel to the form
            UpdatePanel postbackPanel = new UpdatePanel();
            postbackPanel.ID = "cometPostbackPanel";
            postbackPanel.UpdateMode = UpdatePanelUpdateMode.Conditional;
            postbackPanel.ChildrenAsTriggers = false;
            Button postbackButton = new Button();
            postbackButton.ID = "cometPostbackButton";
            postbackButton.Attributes.Add("style", "display:none;");
            postbackPanel.ContentTemplateContainer.Controls.Add(postbackButton);
            this.Form.Controls.Add(postbackPanel);

            //Add the CometRequestor control to the form
            CometRequestor cometRequestor = new CometRequestor();
            cometRequestor.ID = "cometRequestor";
            cometRequestor.PollTime = 1000;
            cometRequestor.SessionID = CometSessionStore.CurrentSessionID;
            this.Form.Controls.Add(cometRequestor);
            base.OnInit(e);
        }

        private void InitializeController()
        {
            this._cometController = CometServer.GetCometController();
        }

        #region ICometPage Members

        public CometController Controller
        {
            get
            {
                return this._cometController;
            }
            set
            {
                this._cometController = value;
            }
        }

        #endregion
    }
}
